package com.hpjz.emrecord.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Service;

import com.hpjz.basicofing.bean.UserBean;
import com.hpjz.basicofing.dao.IUserDao;
import com.hpjz.emrecord.bean.EmRecordBean;
import com.hpjz.emrecord.dao.EmRecordDao;
import com.hpjz.emrecord.service.IEmRecordStatisService;
import com.hpjz.emrecord.web.vo.EmRecordStatisVO;

@Service("emRecordStatisService")
public class EmRecordStatisServiceImpl implements IEmRecordStatisService{

	@Resource
	private EmRecordDao emRecordDao;
	
	@Resource
	private IUserDao userDao;
	
	@Override
	public Map findEmStatis(Map map) {
		
	    String uid = (String) map.get("uid");
	    String platform = (String) map.get("platform");
	    
		//根据用户获取所在的医院，查询所在医院的数据
		UserBean ubean = new UserBean();
		ubean.setUid(uid);
		ubean.setPlatform(platform);
		UserBean user = this.userDao.findOne(ubean);
		EmRecordBean ebean = new EmRecordBean();
		
		/*Long orgId = user.getOrg_id();*/
		Map resultMap = new HashMap<>();
		if (user!=null) {
			ebean.setOr_id(user.getOrg_id());
			List<EmRecordBean> emList = this.emRecordDao.findEmStatis(ebean);
			EmRecordStatisVO vo = this.statisByCondition(emList);
			resultMap.put("data", vo);
		}else{
			resultMap.put("error", "error");
		}
		
		return resultMap;
	}
	
    //统计各种数量(这个是需要重新修改的)
	private EmRecordStatisVO  statisByCondition(List<EmRecordBean> beans){
		 EmRecordStatisVO vo = new EmRecordStatisVO();
		 Integer arrivPersons = 0; //累计急救到
			         
		 Integer	arrivTodayPersons = 0;  //
			         
		 Integer malePersons = 0;   //累计男性
		 Integer maleTodayPersons = 0; //今
			         
		 Integer fmalePersons = 0; //累计女性患
		 Integer fmaleTodayPersons = 0; //
			         
		 Integer illLowPersons = 0;      //
		 Integer illLowTodayPersons = 0; //
			         
		 Integer illMPersons = 0;      //累
		 Integer illMTodayPersons = 0; //今
			         
		 Integer illHPersons = 0;      //累
		 Integer illHTodayPersons = 0; //今
			         
		 Integer illDeathPersons = 0;     
		 Integer illDeathTodayPersons = 0;
		 
		 Integer  emCars = 0;     	//累计5公里以上的   
		 Integer emTodayCars = 0;   	//今日5公里以上的统计 
		                                              
		 Integer  emKCars = 0;      	//累计5公里以下的   
		 Integer  emTodayKCars = 0;   //今日累计5公里以下的 
		 
		 BigDecimal distance = new BigDecimal(5);
		 
		 Date now = new Date();  
		 
		//抢救的总数量
		if (beans!=null) {
			arrivPersons = beans.size();
			 for (EmRecordBean em : beans) {
				    Date  createDate = em.getCreatetime();
				    String sex = em.getPatientInfo().getPatientsex();
				    String illjudgment = em.getIlljudgment();
				    BigDecimal leavedistance = em.getLeavedistance();
				   
				  if (DateUtils.isSameDay(createDate, now)) {
					 arrivTodayPersons++;
				   }
				  if (sex.equals("1")) {
					  malePersons++;
					  if (DateUtils.isSameDay(createDate, now)) {
						 maleTodayPersons++;
					  }
				  } else if(sex.equals("0")){
					  fmalePersons++;
					  if (DateUtils.isSameDay(createDate, now)) {
						  fmaleTodayPersons++;
					   }
				  }
				  
				  if(illjudgment.equals("0001")){
					  illLowPersons++;
					  if (DateUtils.isSameDay(createDate, now)) {
						illLowTodayPersons++;
					  }
				  }else if(illjudgment.equals("0002")){
					  illMPersons++;
					  if (DateUtils.isSameDay(createDate, now)) {
						illMTodayPersons++;
					}
				  }else if(illjudgment.equals("0003")){
					   illHPersons++;
					   if (DateUtils.isSameDay(createDate, now)) {
						illHTodayPersons++;
					  }
				  }else if(illjudgment.equals("0004")){
					  illDeathPersons++;
					  if (DateUtils.isSameDay(createDate, now)) {
						illDeathTodayPersons++;
					 }
				  }
				  
				  /*根据距离统计条数*/
				  //小于5公里
				  if (leavedistance.compareTo(distance)==-1) {
					  emKCars++;
					  if (DateUtils.isSameDay(createDate, now)) {
						  emTodayKCars++;
					  }
				  }else{
					 emCars++;
					 if (DateUtils.isSameDay(createDate, now)) {
						emTodayCars++;
					}
				  }
				  
				}
			
		}
	 
		
		vo.setArrivPersons(String.valueOf(arrivPersons));
		vo.setArrivTodayPersons(String.valueOf(arrivTodayPersons));
		vo.setMalePersons(String.valueOf(malePersons));
		vo.setMaleTodayPersons(String.valueOf(maleTodayPersons));
		vo.setFmalePersons(String.valueOf(fmalePersons));
		vo.setFmaleTodayPersons(String.valueOf(fmaleTodayPersons));
		vo.setIllLowPersons(String.valueOf(illLowPersons));
		vo.setIllLowTodayPersons(String.valueOf(illLowTodayPersons));
		vo.setIllMPersons(String.valueOf(illMPersons));
		vo.setIllMTodayPersons(String.valueOf(illMTodayPersons));
		vo.setIllHPersons(String.valueOf(illHPersons));
		vo.setIllHTodayPersons(String.valueOf(illHTodayPersons));
		vo.setIllDeathPersons(String.valueOf(illDeathPersons));
		vo.setIllDeathTodayPersons(String.valueOf(illDeathTodayPersons));
		vo.setEmCars(String.valueOf(emCars));
		vo.setEmTodayCars(String.valueOf(emTodayCars));
		vo.setEmKCars(String.valueOf(emKCars));
		vo.setEmTodayKCars(String.valueOf(emTodayKCars));
		return vo;
	}
	
	
	

}
